﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace pigs
{
    public class PowerUp
    {

        Texture2D texture;  //image for the powerup
        Vector2 position;   //where the powerup is
        
        public TimeSpan time;   //when it was created
        public string name;     //the name of the powerup
        public Rectangle rectangle;

        public Boolean visible;
        public Boolean active;

        /// <summary>
        /// randomly choose what powerup to make and load image
        /// </summary>
        /// <param name="Position">where to place powerup</param>
        /// <param name="content">content manager so we can load the image</param>
        /// <param name="startTime">when the powerup is created</param>
        public PowerUp(Vector2 Position, ContentManager content, GameTime startTime)
        {
            //randomly choose which powerup to make
            Random rand = new Random(startTime.TotalGameTime.Milliseconds);
            int powerupType = rand.Next(1, 5);

            //make bacon
            if (powerupType == 1)
            {
                this.texture = content.Load<Texture2D>("powerups/bacon");
                this.name = "bacon";
            }

            //make tri bullet
            else if (powerupType == 2)
            {
                this.texture = content.Load<Texture2D>("powerups/tripowerup");
                this.name = "tri";
            }

            //Make flamethrower
            else if (powerupType == 3)
            {
                this.texture = content.Load<Texture2D>("weapons/fireball");
                this.name = "flame";
            }

            //Make bomb
            else if (powerupType == 4)
            {
                this.texture = content.Load<Texture2D>("weapons/bomb2");
                this.name = "bomb";
            }

            this.position.Y = Position.Y - texture.Height / 2;
            this.position.X = Position.X - texture.Width / 2;
            rectangle = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
            this.time = startTime.TotalGameTime;
            visible = true;
            this.active = true;
        }
        
        /// <summary>
        /// update the powerup
        /// </summary>
        /// <param name="gameTime">current game time</param>
        public void Update(GameTime gameTime)
        {
            //if it's been around for 4 seconds make it flash
            if (gameTime.TotalGameTime.Seconds - time.Seconds > 4)
            {
                if (visible)
                {
                    visible = false;
                }
                else
                {
                    visible = true;
                }
            }
            //remove it after 6 seconds
            if (gameTime.TotalGameTime.Seconds - time.Seconds > 6)
            {
                this.active = false;
            }
        }

        /// <summary>
        /// draw the powerupif it's visible
        /// </summary>
        /// <param name="spriteBatch">the spritebatch to include the image in</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (visible)
            {
                spriteBatch.Draw(texture, position, null, Color.White, 0, new Vector2(0, 0), 1f, SpriteEffects.None, 0f);
            }
        }

    }
}
